/**
 * Created by zhaojm on 15/3/21.
 */
game.GameLayer = cc.Layer.extend({

    _road_x_list : [
        130,
        175,
        220
    ],


    _backgroundLayer : null,
    _hubLayer : null,
    _uiLayer : null,

    _bear : null,
    _hero : null,


    _spriteSheet : null,


    _objList : null,


    _timeCount : null,  // 用于加障碍

    _timeCountAll : null,   // 用于计数米数

    _speedUpEffect : null,
    //_speedUpEffect2 : null,
    _speedUpEffectAction : null,
    _speedUpTimeCount : null,

    _isGuide : null,

    ctor:function(){
        this._super();

        cc.spriteFrameCache.addSpriteFrames(res.gameplayer_plist);
        cc.spriteFrameCache.addSpriteFrames(res.cocostudio_plist);
        cc.spriteFrameCache.addSpriteFrames(res.guide_plist);


        this._objList = [];
        this._timeCount = 0;
        this._timeCountAll = 0;
        this._isGuide = false;

        this._backgroundLayer = new game.BackgroundLayer();
        this.addChild(this._backgroundLayer);


        this._spriteSheet = new cc.SpriteBatchNode(res.gameplayer_png);
        this.addChild(this._spriteSheet, 2);

        this._createSpeedUpEffect();



        this._bear = new game.Bear();
        this._bear.addToLayer(this, this._spriteSheet);


        this._hero = new game.Hero();
        this._hero.addToLayer(this, this._spriteSheet);


        this._uiLayer = new game.UILayer();
        this.addChild(this._uiLayer, 2);


        this._hubLayer = new game.HubLayer();
        this.addChild(this._hubLayer, 2);




        if(cc.sys.localStorage.getItem('bear_guide') != '1') {
            //    this.guideLayer = new game.GuideLayer();
            //    this.addChild(this.guideLayer);
            //    this.guideLayer.setSpriteVisible(false);
            //this.guideLayer.setEnabled(false);
            cc.log('first time......');
            this.scheduleOnce(this.beginGuide, 0.5);// 新手引导第一步
        }else{
            this._bear.setScale(0.3);
        }

        this.scheduleUpdate();

    },
    beginGuide:function(){
        this._isGuide = true;
        //this.hero.isCanMove = false;

        //this.guideTask =0;
        //// TODO 手 点击可以跳一次
        //this._guideSprite = new cc.Sprite(cc.spriteFrameCache.getSpriteFrame('guide0.png'));
        //this._guideSprite.setScale(cc.winSize.width / this._guideSprite.getContentSize().width);
        //this._guideSprite.setPosition(cc.p(cc.winSize.width / 2 - this.getPositionX(), cc.winSize.height / 2));
        ////this._guideSprite.setAnchorPoint(cc.p(0, 0));
        //this.addChild(this._guideSprite, 5);
        //this._guideButton = new cc.MenuItemLabel(new cc.LabelTTF('下一步', 'Arial', 12), this.onGuideButtonCallback);
        //this._guideButton.setAnchorPoint(cc.p(1, 1));
        ////this.addChild(this._guideButton, 5);
        //this._guideButton.setPosition(cc.p(cc.winSize.width - 10 - this.getPositionX(), cc.winSize.height - 10));
        //this._menu = new cc.Menu(this._guideButton);
        //this._menu.setPosition(cc.p(0, 0));
        //this.addChild(this._menu, 5);
        //this.touchLayer.setTouchEnabled(false);
        ////this.guideLayer.setSpriteVisible(true);
        ////this.guideLayer.setSpritePosition(cc.p(cc.winSize.width / 2 - this.getPositionX(), cc.winSize.height / 2));
        ////this.guideLayer.setEnabled(true);
        //this._hubLayer.setTouchEnabled(false);
        this._uiLayer.startGuide();

    },


    _createSpeedUpEffect : function(){
        this._speedUpEffect = new cc.Sprite('#effect/0.png');
        this._speedUpEffect.setPosition(cc.p(cc.winSize.width * 0.5, cc.winSize.height * 0.5));
        //this._speedUpEffect2 = new cc.Sprite('#effect/0.png');
        //this._speedUpEffect2.setPosition(cc.p(cc.winSize.width * 0.5, cc.winSize.height * 0.5));
        this._speedUpEffectAction = new cc.RepeatForever(new cc.Animate(
            new cc.Animation([0, 1].map(function (i) {
                return cc.spriteFrameCache.getSpriteFrame("effect/" + i + ".png");
            }), 0.07)
        ));

        this._spriteSheet.addChild(this._speedUpEffect);
        //this._spriteSheet.addChild(this._speedUpEffect2);
        this._speedUpEffect.setVisible(false);
        //this._speedUpEffect2.setVisible(false);
    },

    startSpeedUp : function(){
        this._speedUpTimeCount = 10;
        this._speedUpEffect.setVisible(true);
        this._speedUpEffect.stopAllActions();
        this._speedUpEffect.runAction(this._speedUpEffectAction);
        //this._speedUpEffect2.setVisible(true);
        //this._speedUpEffect2.stopAllActions();
        //this._speedUpEffect2.runAction(this._speedUpEffectAction);
    },



    update : function(dt){
        this._hero.update(dt);
        if(this._isGuide){
            return;
        }


        for(var i = 0; i < this._objList.length; i++){
            this._objList[i].update(dt);
        }

        this.addObj(dt);


        this._timeCountAll += dt;

        this._uiLayer.setText(this._timeCountAll * 1);

        // 处理加速的效果
        if(this._speedUpTimeCount > 0){
            this._speedUpTimeCount -= dt;
            //this._bear.setScale();
        }else if(this._speedUpTimeCount <= 0){
            this._speedUpTimeCount = 0;
            this._speedUpEffect.stopAllActions();
            this._speedUpEffect.setVisible(false);
            //this._speedUpEffect2.stopAllActions();
            //this._speedUpEffect2.setVisible(false);
        }



    },


    addObj : function(dt){

        this._timeCount += dt;
        var interval = Math.random() * 10 + 2;
        interval = this._timeCountAll < 10 ? interval : Math.random() * 2 + 1;
        interval = this._timeCountAll < 30 ? interval : Math.random() * 1 + 1;
        interval = this._timeCountAll < 50 ? interval : Math.random() * 1 + 0.5;
        interval = this._timeCountAll < 80 ? interval : Math.random() * 0.5 + 0.5;
        interval = this._timeCountAll < 100 ? interval : Math.random() * 0.5 + 0.5;
        interval = this._timeCountAll < 150 ? interval : Math.random() * 0.4 + 0.5;
        interval = this._timeCountAll < 200 ? interval : Math.random() * 0.3 + 0.4;
        interval = this._timeCountAll < 250 ? interval : Math.random() * 0.2 + 0.3;
        interval = this._timeCountAll < 300 ? interval : Math.random() * 0.1 + 0.2;

        if(this._timeCount >= interval){
            this._timeCount = 0;
            if(this._hero._pinconeCount >= 15){
                (new game.HastePotion()).addToLayer(this);
                this._hero._pinconeCount -= 15;
                return;
            }
            var type = Math.floor(Math.random() * 3);
            cc.log('type==', type);
            switch (type){
                case 0:
                    (new game.Wood()).addToLayer(this);
                    break;
                case 1:
                    (new game.PineCone()).addToLayer(this);
                    break;
                case 2:
                    (new game.Stone()).addToLayer(this);
                    break;
                default :
                    break;
            }
        }
    },

    gameOver:function(){

        this._hero.gameOver();
        this._bear.gameOver();

        this._backgroundLayer.gameOver();
        var self = this;
        (new game.GameOverEffect()).addToLayer(this, cc.p(cc.winSize.width / 2, cc.winSize.height / 2),
            function(){
            cc.log('on game over....');


                var data = {};
                data.scoreNum = self._timeCountAll * 10;
                //data.distance = Math.floor(that.parent.distance);
                var gameOverScene = new game.GameOverScene();
                gameOverScene.setData(data);
                cc.director.runScene(gameOverScene);
            //var gameOverScene = new game.GameOverScene();
            //gameOverScene.setData(self._timeCountAll);
            //cc.director.runScene(gameOverScene);
        });



    },

});